<?php

  class File {
    
    function __construct ($name) {
      $path_parts = pathinfo($name);
      $this->mtime = filemtime($name);
      $this->basename = $path_parts['basename'];
      $this->dirname = $path_parts['dirname'];
      $this->extension = strtolower($path_parts['extension']);
      $this->filename = $path_parts['filename'];
      $this->path = $name;
      $this->root_dirname = preg_replace('/^' . str_replace(S, '\\'.S,  ROOT_DIR) . '/', '', $this->dirname);
      $this->root_path = $this->root_dirname . '/' . $this->basename;
      $this->public_dirname = '/' . preg_replace('/^' . str_replace(S, '\\'.S,  ROOT_DIR) . 'public[\\/]/', '', $this->dirname);
      $this->public_path = $this->public_dirname . '/' . $this->basename;
      //$this->mime_content_type = mime_content_type($this->path);
      $this->is_image();
    }
    
    function is_type($type) {
      if (is_array($type)) {
        if (in_array($this->extension, $type)) {
          return true;
        } else {
          return false;
        }
      } else {
        if ($this->extension == $type) {
          return true;
        } else {
          return false;
        }
      }
    }
    
    function is_trivial() {
      if ($this->basename == 'Thumbs.db') {
        return true;
      }
      return false;
    }
    
    function is_image() {
      $array = getimagesize($this->path);
      if ($array['mime']) {
        $this->content_mime_type = $array['mime'];
        return true;
      } else {
        return false;
      }
    }
    
    
    
		
		function resize($new_max_width, $new_max_height, $params = array()) {
		  // Return if image type/extension is not supported
			if (!$this->is_image()) return false;
			
		  $path_from = $this->path;
		  //pc($path_from);
			$new_path = $path_from;
		  
			
			// Looping through parameters - if any...
		  foreach ($params as $name => $value) {
		    switch ($name) {
		      case 'new_path':
		          $$name = $value;
		        break;
		    }
		  }
		  
		  switch ($this->content_mime_type) {
		    case 'image/jpeg':
		        $img_socket = imagecreatefromjpeg($path_from);
		      break;
		    case 'image/png':
		        $img_socket = imagecreatefrompng($path_from);
		      break;
		    case 'image/gif':
		        $img_socket = imagecreatefromgif($path_from);
		      break;
		    case 'image/wbmp':
		        $img_socket = imagecreatefromwbmp($path_from);
		      break;
		    default:
		        return false;
		      break;
		  }
		  
  		$this->width = imagesx($img_socket);
  		$this->height = imagesy($img_socket);
			
			// Calculating ratios
			$ratio = $this->width / $this->height;
			$expected_ratio = $new_max_width / $new_max_height;
			
			
			
			// Calculating new dimensions
			if ($ratio > $expected_ratio) {
				$img_new_width = $new_max_width;
				$img_new_height = ($new_max_width  / $ratio);
			} else {
				$img_new_width = ($new_max_height * $ratio);
				$img_new_height = $new_max_height ;
			}
			
			// Creating new resized JPG
			if ($img_new_height > $this->height) {
			  $img_new_height = $this->height;
			  $img_new_width = $this->width;
			}
  			$temp_img_socket = imagecreatetruecolor($img_new_width, $img_new_height);
  			imagecopyresampled($temp_img_socket, $img_socket, 0, 0, 0, 0, $img_new_width, $img_new_height, $this->width, $this->height);
  			
  			
		  
		  switch ($this->content_mime_type) {
		    case 'image/jpeg':
		        $success = imagejpeg($temp_img_socket, $new_path, 80);
		      break;
		    case 'image/png':
		        $success = imagepng($temp_img_socket, $new_path);
		      break;
		    case 'image/gif':
		        $success = imagegif($temp_img_socket, $new_path, 80);
		      break;
		    case 'image/wbmp':
		        $success = imagewbmp($temp_img_socket, $new_path);
		      break;
		    default:
		        return false;
		      break;
		  }
		  
		  if ($success) {
        Akrudo::raise_info("      [file   RESIZE SUCCESFULL: $new_path");
			  $file = new File ($new_path);
			  return $file;
			}
			return false;
		}
		
		
		
		public function GetJpgWidth($path_from) {
			if (!$img = imagecreatefromjpeg($path_from)) {
				return false;
			}
			
			// Getting JPG's dimensions data
			$imgW = imagesx($img);
			return $imgW;
		}
		
		public function GetJpgHeight($path_from) {
			if (!$img = imagecreatefromjpeg($path_from)) {
				return false;
			}
			
			// Getting JPG's dimensions data
			$imgH = imagesy($img);
			return $imgH;
		}
    
  }
  
?>